package com.tos.controller;

import com.alibaba.fastjson.JSONArray;
import com.tos.pojo.*;
import com.tos.service.CityService;
import com.tos.service.FlightService;
import com.tos.service.TicketService;
import com.tos.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;


    @RequestMapping("/getTickets")
    public List<Ticket> getTicketsByPassenger(@RequestBody TicketQuery ticketQuery) {
        HashMap<String, Object> map = new HashMap<>();
        String cardId = ticketQuery.getCardId();
        System.out.println(ticketQuery.getSrcCity());
        System.out.println(ticketQuery.getDstCity());
        System.out.println(cardId);
        System.out.println(ticketQuery.getStartTime());

        map.put("cardId",cardId);
        map.put("srcCity",ticketQuery.getSrcCity());
        map.put("dstCity",ticketQuery.getDstCity());
        map.put("queryDate",ticketQuery.getStartTime());

        List<Ticket> ticketList = ticketService.getTicketsByPassenger(map);

        return ticketList;
    }

    @RequestMapping("/refund")
    public String  refund(@RequestBody TicketId ticketId) {

        System.out.println(ticketId);
        boolean success= ticketService.refund(ticketId.getTicketId());
        if(success){
            return "success";
        }
        else{
            return "退票失败，请稍后再试";
        }

    }
    @RequestMapping("/queryTicketByCardId")
    public List<Ticket> queryTicketByCardId(@RequestBody Passenger passenger){
        return ticketService.getTickets(passenger.getCardId());
    }
}
